/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.widget;

import java.util.List;

import sw6.digipecs.R;
import sw6.digipecs.model.ExtImage;
import sw6.digipecs.model.PECSBook.PECSBookChangeListener;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;

public class ExtImageAdapter extends BaseAdapter implements PECSBookChangeListener {
	private List<ExtImage> mImageArrayList;
	
	protected Context mContext;

	private LayoutInflater mLayoutInflater; 	
	
	public ExtImageAdapter (Context context, List<ExtImage> images)
	{
		this.mContext=context; 
		this.mImageArrayList=images;
		
		String infService = Context.LAYOUT_INFLATER_SERVICE; 
		this.mLayoutInflater = (LayoutInflater) context.getSystemService(infService);
	}
	@Override
	public int getCount() {
		return mImageArrayList.size(); 
	}

	@Override
	public Object getItem(int position) {
		return mImageArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position; 
	}
	
	protected ExtImageView createView()  {
		ExtImageView imageView;
		imageView = new ExtImageView(mContext);
        
        return imageView;
	}
	
	protected FrameLayout convertView(View conv, ViewGroup parent) {
		FrameLayout view;
		
		if (conv == null) {
            view = (FrameLayout) mLayoutInflater.inflate(R.layout.placeholder, parent, false);
        } else {
        	view = (FrameLayout) conv;
        	view.removeAllViews();
        }
		
		return view;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		FrameLayout view;
		// TODO creating imageview each time is kinda good: draggable layout may depend on this, because an image can be dragged, while page is changed -- that is, this adapter reloads all pictures, and reusing images could create a problem here
		ExtImageView imageView = createView();
		ExtImage image = mImageArrayList.get(position);
		
        view = convertView(convertView, parent);
        
        //Log.v("sw6", "nu kaldes setImages" + imageView.toString());
        //TODO Why is an ExtView created twice ??
        imageView.setImage(image);
        view.addView(imageView);
        
        return view;
	}
	
	@Override
	public void bookChanged() {
		notifyDataSetChanged();
	}
}
